package com.corechina.apex_osswords.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.domain.dao.CoreEnterpriseMapper;
import com.corechina.apex_osswords.domain.entity.CoreEnterprise;
import com.corechina.apex_osswords.domain.vo.CoreEnterpriseVo;
import com.corechina.apex_osswords.service.ICoreEnterpriseService;
import com.corechina.apex_osswords.service.ICoreFilesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 企业表 服务实现类
 * </p>
 *
 * @author cc
 * @since 2022-12-09
 */
@Service
public class CoreEnterpriseServiceImpl extends ServiceImpl<CoreEnterpriseMapper, CoreEnterprise> implements ICoreEnterpriseService {

    @Autowired
    private CoreEnterpriseMapper coreEnterpriseMapper;

    @Autowired
    private ICoreFilesService iCoreFilesService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Response add(CoreEnterprise coreEnterprise) {
        coreEnterprise.setSerialNumber(this.getSerialNumber());
        if (coreEnterpriseMapper.insert(coreEnterprise) > 0) {
            return Response.ok();
        }
        return Response.error();
    }

    @Override
    public Response getList(String name, Integer pageIndex, Integer pageSize) {
        IPage<CoreEnterprise> enterpriseIPage = coreEnterpriseMapper.getList(new Page(pageIndex, pageSize), name);
        return Response.ok(enterpriseIPage.getRecords(), enterpriseIPage.getTotal());
    }

    public String getSerialNumber() {
        String serialNumber = coreEnterpriseMapper.getMaxSerialNumber();
        if (serialNumber == null || !serialNumber.contains("E")) {
            return "E001";
        }
        serialNumber = StringUtils.substringAfter(serialNumber, "E");
        serialNumber = "E" + String.format("%03d", Integer.parseInt(serialNumber) + 1);
        QueryWrapper<CoreEnterprise> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("serial_number", serialNumber);
        if (coreEnterpriseMapper.selectCount(queryWrapper) > 0) {
            serialNumber = getSerialNumber();
        }
        return serialNumber;
    }

    @Override
    public Response getOne(Integer id) {
        CoreEnterprise coreEnterprise = coreEnterpriseMapper.selectById(id);
        CoreEnterpriseVo coreEnterpriseVo = new CoreEnterpriseVo();
        if(coreEnterprise != null ){
            BeanUtils.copyProperties(coreEnterprise, coreEnterpriseVo);
            if(coreEnterprise.getLogo() != null){
                coreEnterpriseVo.setLogoBase64(iCoreFilesService.getByGuidKey(coreEnterpriseVo.getLogo()).getImageBase64());
            }
        }
        return Response.ok(coreEnterpriseVo);
    }
}
